$(function($) {

	bindCheckBox("myCheckedAll", "myChecked");

	$("#queryForm").validate();

	$("#_startTime,#_endTime").datepicker({
		format : 'yyyy-mm-dd'
	});

	/**
	 * 已加载的托盘绑定释放事件
	 */
	$("table[name=orderTab]").on('click', "a[name=btn-release]", function() {
		operate(1, $(this).attr("release"));
	});
	/**
	 * 批量释放托盘
	 */
	$("#btn-releaseAll").click(function(){
		var checkArray = $("input[name='myChecked']:checked");
		var lpnNos = "";
		for (var i = 0; i < checkArray.length; i++) {
			lpnNos += checkArray[i].value+","; 
		}
		if(lpnNos.indexOf(",") != -1){
			lpnNos = lpnNos.substring(0, lpnNos.length-1);
		}
		operate(1, lpnNos);
	});
	/**
	 * 已加载的托盘绑定禁用事件
	 */
	$("table[name=orderTab").on('click', "a[name=btn-off]", function() {
		operate(3, $(this).attr("off"));
	});
	/**
	 * 批量禁用托盘
	 */
	$("#btn-offAll").click(function(){
		var checkArray = $("input[name='myChecked']:checked");
		var lpnNos = "";
		for (var i = 0; i < checkArray.length; i++) {
			lpnNos += checkArray[i].value+","; 
		}
		if(lpnNos.indexOf(",") != -1){
			lpnNos = lpnNos.substring(0, lpnNos.length-1);
		}
		operate(3, lpnNos);
	});
	function operate(toStatus, lpnNos) {
		$
				.ajax({
					type : 'post',
					url : '../v2_pallet/batchOperate',
					async : false,
					data : {
						toStatus : toStatus,
						lpnNos : lpnNos
					},
					dataType : 'json',
					success : function(data) {
						var status = data.status;
						var Status = "";
						if (status == "success") {
							updateStatus(toStatus,lpnNos) ;
						}
					},
					error : function() {
						jAlert("网络繁忙，请稍后重试...", "温馨提示");
					}
				});
	}
	//批量界面显示修改
	function updateStatus(toStatus,lpnNos){
		if (toStatus == 1) {
			Status = "空闲";
		} else if (toStatus == 3) {
			Status = "禁用";
		}
		var lpnNos2;
		if (lpnNos.indexOf(",") != -1) {
			lpnNos2 = lpnNos.split(",");
		} else {
			lpnNos2 = [ lpnNos ];
		}
		for (var i = 0; i < lpnNos2.length; i++) {
			$("#status_" + lpnNos2[i]).html(Status);
			var a = $("#status_" + lpnNos2[i]).parent("tr").find("td").eq(2).html();
			if (Status == "空闲") {
				$("#operate_" + lpnNos2[i])
						.find("a").eq(2).replaceWith(
								"<a class='btn btn-primary' name='btn-off' off='"+lpnNos2[i]+"'>禁用</a>");
			} else {
				$("#operate_" + lpnNos2[i])
						.find("a").eq(2).replaceWith(
								"<a class='btn btn-primary' name='btn-release' release='"+lpnNos2[i]+"'>释放</a>");
			}

		}
	}
	//打印条码
	$("a[name=btn_printCode]").click(function(){
		var lpnNo = $(this).parent("td").parent("tr").find("td").eq(2).html();
		$("#lpnNos").val(lpnNo);
		packFormPrinter("printerBody");
	});
	//批量打印条码
	$("#btn_printCodeAll").click(function(){
		var checkArray = $("input[name='myChecked']:checked");
		var lpnNos = "";
		for (var i = 0; i < checkArray.length; i++) {
			lpnNos += checkArray[i].value+","; 
		}
		if(lpnNos.indexOf(",") != -1){
			lpnNos.substring(0, lpnNos.length-1);
		}
		$("#lpnNos").val(lpnNos);
		packFormPrinter("printerBody");
	});
	// 打印
	$("#beginPrint").click(function(){
		var lpnNos = $("#lpnNos").val();
		var printName = $("#printerBody").val();
		var myCodes = lpnNos.split(",");
		for (var i = 0; i < myCodes.length; i++) {
			if (myCodes[i].length > 0)
				printBarCode(printName, myCodes[i]);
		}
	});
	function printBarCode(printName, myCode) {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document
				.getElementById('LODOP_EM'));
		LODOP.PRINT_INIT(myCode);
		LODOP.SET_PRINT_PAGESIZE(1, 600, 320, "");
		LODOP.ADD_PRINT_BARCODE(25, 10, 200, 45, "128B", myCode);
		LODOP.SET_PRINT_STYLE("FontName", "SimHei");
		LODOP.SET_PRINT_STYLE("FontSize", 25);
		LODOP.ADD_PRINT_TEXT(70, 28, 200, 100, myCode);
		if (LODOP.SET_PRINTER_INDEXA(printName)) {
			LODOP.PRINT();
		}
	}
});